package com.yehuishou.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.exceptions.ClientException;
import com.yehuishou.pojo.Identity;
import com.yehuishou.pojo.User;
import com.yehuishou.service.IdentityService;
import com.yehuishou.service.UserService;
import com.yehuishou.util.ALYSmsUtil;
import com.yehuishou.util.IndustrySMSUtil;
import com.yehuishou.util.MD5Util;

/**
 * 
 * @author zxb
 *
 */
@Controller
public class LoginController {

	@Autowired
	private UserService userService;
	@Autowired
	private IdentityService identityService;

	@RequestMapping("logout")
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		return "index";
	}

	// 登录
	@ResponseBody
	@RequestMapping("denlu")
	public Map<String, Object> login(String phone, String password, String code, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		if (password == null && code == null) {
			map.put("msg", "请输入密码验证码");
			map.put("success", false);
		} else {
			if (password == null && code != null) {
				String codes = (String) session.getAttribute("code");
				if (code.equals(codes)) {
					Integer uid = userService.findByPhone(phone);
					if (uid != null) {
						session.setAttribute("uid", uid);
					} else {
						User user = new User();
						user.setPhone(phone);
						user.setCreated(new Date());
						user.setUpdated(new Date());
						Integer i = userService.add(user);
						uid = user.getUid();
						Identity ide = new Identity();
						ide.setId(uid);
						ide.setIstate(0);
						identityService.add(ide);
						session.setAttribute("uid", uid);
					}
					map.put("success", true);
				} else {
					map.put("msg", "验证码错误");
					map.put("success", false);
				}
			} else {
				User user = userService.findLogin(phone, MD5Util.MD5(password));
				if (user == null) {
					map.put("msg", "手机号或密码错误");
					map.put("success", false);
				} else {
					session.setAttribute("user", user);
					map.put("success", true);
				}
			}
		}
		return map;
	}

	@RequestMapping("pw")
	public String pw() {
		return "forget-pw";
	}

	// 设置密码
	@ResponseBody
	@RequestMapping("mima")
	public boolean mima(String password, HttpSession session) {
		Integer uid = (Integer) session.getAttribute("uid");
		User user = new User();
		user.setUid(uid);
		user.setPassword(MD5Util.MD5(password));
		user.setUpdated(new Date());
		userService.Update(user);
		User us = userService.findById(uid);
		session.setAttribute("user", us);
		return true;
	}

	@RequestMapping("/")
	public String index() {
		return "index";
	}

	// 发送验证码
	@ResponseBody
	@RequestMapping("send")
	public Map<String, Object> send(String phone, HttpSession session) throws ClientException {
		// 生成6位随机验证码
		int cound = (int) ((Math.random() * 9 + 1) * 100000);
		String code = cound + "";
		session.setAttribute("code", code);
		System.out.println(code);
		ALYSmsUtil.sendSms(phone, code);
		// IndustrySMSUtil.execute(phone, code);
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "success");
		return map;
	}

}
